﻿package com.projects.xenia.journals_activities;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.projects.xenia.R;
import com.projects.xenia.R.drawable;
import com.projects.xenia.R.id;
import com.projects.xenia.R.layout;
import com.projects.xenia.global_modules.MainGlobalModule;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


	public class Activity_Journal_Invoices extends ListActivity {

		private EfficientAdapter adap;

		private String OrderId;
		
		private String OrderDescr;			
		private String CustomerDescr;		
		
		private TextView InvoiceJournalHeader;
		
		private String id;
		private String number;
		private Date date_save;				
		private int ismark; 
		private int act; 
		private Double total_sum; 
		private String comment;		
		
		private Cursor InvoiceJournalCursor;
		
		private Bundle DocExtra;
		
		public void GetInvoiceJournalCursor () {		
			
			try {
				InvoiceJournalCursor = MainGlobalModule.DBE.rawQuery("select id, number, date_save, ismark, act, total_sum, comment from xref_invoices where id = ? order by number asc", new String []{OrderId});	
			} catch (Exception e) {
				e.printStackTrace();
			}
				
					
			
		}
		
		@Override
		public void onCreate(Bundle savedInstanceState) {
			
			super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_NO_TITLE);            
	        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
			setContentView(R.layout.invoice_journal_form);
			
			DocExtra = getIntent().getExtras();
			OrderId = DocExtra.getString("OrderId");
			OrderDescr = DocExtra.getString("OrderDescr");			
			CustomerDescr = DocExtra.getString("OrderCustomerDescr");
			
			InvoiceJournalHeader = (TextView)findViewById(R.id.InvoiceJournalHeader);
			InvoiceJournalHeader.setText(String.format("Накладные %s", ""));
			
			GetInvoiceJournalCursor ();
			
			adap = new EfficientAdapter(this);
			setListAdapter(adap);
			
			MainGlobalModule.setBackGround(getListView());
			
		}
								
			
	    public boolean onCreateOptionsMenu (Menu menu){
	    	menu.add(Menu.NONE,1,Menu.NONE,"Выход").setIcon(R.drawable.st_exit);
			return true;    	
	    }
	    
	    public boolean onOptionsItemSelected (MenuItem item){
	    	switch (item.getItemId()) {
	    		case 1:
	    			finish();
	    			return true;
	    	}	    		
	    			return false;    	
	    	    
	    }
		
		public class EfficientAdapter extends BaseAdapter  {
		
			private LayoutInflater mInflater;
			public Context context;
			 
			public EfficientAdapter(Context context) { 
				mInflater = LayoutInflater.from(context);
				this.context = context;
			}
			 
			public View getView(final int position, View convertView, ViewGroup parent) {

				ViewHolder holder;
			
				if (convertView == null) {
				
					convertView = mInflater.inflate(R.layout.invoice_journal_list, null);
			
					holder = new ViewHolder();				
					
					holder.InvoicesInvoiceJournal_Status = (ImageView) convertView.findViewById(R.id.InvoiceJournal_State);					
					holder.InvoicesInvoiceJournal_Date = (TextView) convertView.findViewById(R.id.InvoiceJournal_Date);
					holder.InvoicesInvoiceJournal_Num = (TextView) convertView.findViewById(R.id.InvoiceJournal_Num);
					holder.InvoicesInvoiceJournal_Sum = (TextView) convertView.findViewById(R.id.InvoiceJournal_Sum);
					holder.InvoicesInvoiceJournal_Comment = (TextView) convertView.findViewById(R.id.InvoicesInvoiceJournal_Comment);
					convertView.setTag(holder);
			
			}
			
			else
				
				{
					holder = (ViewHolder) convertView.getTag();
				}
			
				getItem(position);
			
				
				
				switch (act) {
				case 0:
					{holder.InvoicesInvoiceJournal_Status.setImageResource(R.drawable.ic_bounddoc);}			
					break;					
				case 1:
					{holder.InvoicesInvoiceJournal_Status.setImageResource(R.drawable.ic_invoicedoc);}			
					break;					
				default:
					break;
				}
				
				switch (ismark) {				
				case 1:
					{holder.InvoicesInvoiceJournal_Status.setImageResource(R.drawable.ic_deldoc);}			
					break;					
				default:
					break;
				}				
				
				holder.InvoicesInvoiceJournal_Date.setText(String.format("от %td.%tm.%ty ",date_save,date_save,date_save));		
				holder.InvoicesInvoiceJournal_Num.setText(number);
				holder.InvoicesInvoiceJournal_Sum.setText(String.format("%.2f", total_sum));
				holder.InvoicesInvoiceJournal_Comment.setText(comment);
			
				return convertView;
			
			}
			 
			class ViewHolder {			

				ImageView InvoicesInvoiceJournal_Status;
				TextView InvoicesInvoiceJournal_Date;		
				TextView InvoicesInvoiceJournal_Num;				
				TextView InvoicesInvoiceJournal_Comment;			
				TextView InvoicesInvoiceJournal_Sum;
				
			}
			 
					 
			@Override
			public long getItemId(int position) {

				return 0;
				
			}
			 
			@Override
			public int getCount() {

				return InvoiceJournalCursor.getCount();
			
			}
			 
			@Override
			public Object getItem(int position) { 
				
				InvoiceJournalCursor.moveToPosition(position);
				
				id = InvoiceJournalCursor.getString(0);
				number = InvoiceJournalCursor.getString(1);
				date_save = MainGlobalModule.SqlStringToDate(InvoiceJournalCursor.getString(2));				
				ismark = InvoiceJournalCursor.getInt(3); 
				act = InvoiceJournalCursor.getInt(4); 
				total_sum= InvoiceJournalCursor.getDouble(5); 
				comment = InvoiceJournalCursor.getString(6);				
				
				return id;
			
			}
		}
		
	}
